package org.Utils;

import java.util.StringJoiner;

public class IOUtils {
    public static void printEquals() {
        System.out.println("==================================================================================");
    }

    public static String getByteArrays(byte[] b) {
        StringBuilder s = new StringBuilder();
        for (byte value : b) {
            s.append(String.format("%X ", value));
        }
        return s.toString();
    }

    public static String buildInsertSql(String table, String[] fields) {
        StringBuilder s = new StringBuilder();
        StringBuilder stringTailBuilder = new StringBuilder();

        s.append("INSERT INTO ");
        s.append(table);
        s.append(" (");

        stringTailBuilder.append("(");

        for (String field : fields) {
            s.append(field)
                    .append(", ");
            stringTailBuilder.append("?, ");
        }

        // 注意去掉最后的", ":
        s.delete(s.length() - 2, s.length())
                .append(")");
        stringTailBuilder.delete(stringTailBuilder.length() - 2, stringTailBuilder.length())
                .append(")");
        s.append(" VALUES ").append(stringTailBuilder.toString());

        return s.toString();
    }

    public static String buildSelectSql(String table, String[] fields) {
        StringJoiner stringJoiner = new StringJoiner(", ", "SELECT ", " FROM %s");
        for (String string: fields) {
            stringJoiner.add(string);
        }
        return String.format(stringJoiner.toString(), table);
    }
}
